package com.example.wangziqiao.myapplication.service;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.example.wangziqiao.myapplication.MainActivity;
import com.example.wangziqiao.myapplication.util.HttpClientUtils;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import javax.security.auth.login.LoginException;

import cz.msebera.android.httpclient.Header;

/**
 * 登录服务
 * Created by wangziqiao on 2016/4/7.
 */
public class LoginServiceImpl implements LoginService {

    private static final String LOGIN_VALIDATE_URL = "http://portal.yihaodian.com.cn/validateBackOperator.action";

    private static final String LOGIN_URL = "http://portal.yihaodian.com.cn/login.action";
    private  ProgressDialog dialog;
    @Override
    public void TryLogin(final Context context, final String username, final String password) {
        dialog = new ProgressDialog(context);
        dialog.setMessage("登录中……");
        dialog.show();
        RequestParams params = new RequestParams();
        params.put("systemId", "20");
        params.put("backOperatorDto.nextNoLogin", "");
        params.put("redirectURL", "http://portal.yihaodian.com.cn/portalToOtherSystem.action?systemId=3");
        params.put("backOperatorDto.userName", username);
        params.put("backOperatorDto.password", password);
        params.put("backOperatorDto.tokenId", "");
        params.put("radio", "on");
        HttpClientUtils.post(LOGIN_VALIDATE_URL, params, new TextHttpResponseHandler() {

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                dialog.cancel();
                Toast.makeText(context, "网络连接超时", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if(Integer.valueOf(responseString)>0){
                    doTrueLogin(context,username,password);
                }else{
                    dialog.cancel();
                    Toast.makeText(context, "登陆失败，用户名或密码错误！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void doTrueLogin(final Context context, String username, String password) {
        // 创建请求参数的封装的对象
        RequestParams params = new RequestParams();
        // 设置请求的参数名和参数值
        params.put("backOperatorDto.userName", username);
        params.put("backOperatorDto.password", password);
        params.put("radio", "on");
        params.put("backOperatorDto.tokenId", "");
        params.put("systemId", 20);
        params.put("backOperatorDto.nextNoLogin", 1);
        params.put("redirectURL", "http://portal.yihaodian.com.cn/portalToOtherSystem.action?systemId=3");
        HttpClientUtils.post(LOGIN_URL, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                dialog.cancel();
                Toast.makeText(context, "登陆失败，请检查用户名密码", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                Document dom = Jsoup.parse(responseString);
                Elements iframes = dom.select("iframe");
                if(iframes!=null){
                    String url1 = iframes.get(0).attr("src");
                    doStep2(context,url1);
                }else{
                    onFailure(statusCode,headers,responseString,new LoginException());
                }
            }
        });
    }

    private void doStep2(final Context context, String url1) {
        HttpClientUtils.get(url1, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, MainActivity.class);
                context.startActivity(intent);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                dialog.cancel();
                Toast.makeText(context, "登录失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
